using UnityEngine;

namespace Exploder.Demo
{
	public class GrenadeObject : MonoBehaviour
	{
		public AudioClip ExplosionSound;

		public AudioSource SourceExplosion;

		public ParticleSystem ExplosionEffect;

		public Light Flash;

		public bool ExplodeFinished;

		public bool Impact;

		private bool throwing;

		private float explodeTimeoutMax;

		private bool explosionInProgress;

		public ExploderObject exploder;

		private int flashing;

		public void Throw()
		{
			Impact = false;
			throwing = true;
			explodeTimeoutMax = 5f;
			ExplodeFinished = false;
			flashing = -1;
		}

		public void Explode()
		{
			if (!explosionInProgress)
			{
				explosionInProgress = true;
				throwing = false;
				if (!Impact)
				{
					explodeTimeoutMax = 5f;
					return;
				}
				exploder.transform.position = base.transform.position;
				exploder.ExplodeSelf = false;
				exploder.UseForceVector = false;
				exploder.Radius = 5f;
				exploder.TargetFragments = 200;
				exploder.Force = 20f;
				exploder.ExplodeRadius(OnExplode);
				ExplodeFinished = false;
			}
		}

		private void OnExplode(float timeMS, ExploderObject.ExplosionState state)
		{
			if (state == ExploderObject.ExplosionState.ExplosionStarted)
			{
				ExploderUtils.SetVisible(base.gameObject, status: false);
				SourceExplosion.PlayOneShot(ExplosionSound);
				Flash.gameObject.transform.position = base.gameObject.transform.position;
				Flash.gameObject.transform.position += Vector3.up;
				flashing = 10;
				ExplosionEffect.gameObject.transform.position = base.gameObject.transform.position;
				ExplosionEffect.Emit(1);
			}
			if (state == ExploderObject.ExplosionState.ExplosionFinished)
			{
				ExplodeFinished = true;
				explosionInProgress = false;
			}
		}

		private void OnCollisionEnter(Collision other)
		{
			Impact = true;
			if (!throwing && !ExplodeFinished)
			{
				Explode();
			}
		}

		private void Update()
		{
			if (flashing >= 0)
			{
				if (flashing > 0)
				{
					Flash.intensity = 5f;
					flashing--;
				}
				else
				{
					Flash.intensity = 0f;
					flashing = -1;
				}
			}
			explodeTimeoutMax -= Time.deltaTime;
			if (!ExplodeFinished && explodeTimeoutMax < 0f)
			{
				Impact = true;
				Explode();
			}
		}
	}
}
